<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <!-- <el-form-item label="企业的唯一标识码，用于区分不同客户" prop="enterpriseCode">
        <el-input
          v-model="queryParams.enterpriseCode"
          placeholder="请输入企业的唯一标识码，用于区分不同客户"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="企业的全称或常用名称" prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入企业的全称或常用名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户来源渠道，例如线上推广、线下活动、合作伙伴推荐等" prop="source">
        <el-input
          v-model="queryParams.source"
          placeholder="请输入客户来源渠道，例如线上推广、线下活动、合作伙伴推荐等"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户的业务类型分类，如数据中心、零售、制造业等" prop="customerCategory">
        <el-input
          v-model="queryParams.customerCategory"
          placeholder="请输入客户的业务类型分类，如数据中心、零售、制造业等"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户的重要等级，如A级、B级、C级，用于资源分配" prop="customerLevel">
        <el-input
          v-model="queryParams.customerLevel"
          placeholder="请输入客户的重要等级，如A级、B级、C级，用于资源分配"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户方的主要联系人姓名" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入客户方的主要联系人姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="联系人的手机号码" prop="mobileNumber">
        <el-input
          v-model="queryParams.mobileNumber"
          placeholder="请输入联系人的手机号码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="负责该客户的内部销售或服务经理" prop="customerManager">
        <el-input
          v-model="queryParams.customerManager"
          placeholder="请输入负责该客户的内部销售或服务经理"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="与客户签订的合同总金额" prop="contractAmount">
        <el-input
          v-model="queryParams.contractAmount"
          placeholder="请输入与客户签订的合同总金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="会计期间开始时的应收账款余额" prop="initialReceivable">
        <el-input
          v-model="queryParams.initialReceivable"
          placeholder="请输入会计期间开始时的应收账款余额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="当期应收取的款项总额" prop="receivableAmount">
        <el-input
          v-model="queryParams.receivableAmount"
          placeholder="请输入当期应收取的款项总额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="当期实际收到的款项总额" prop="receivedAmount">
        <el-input
          v-model="queryParams.receivedAmount"
          placeholder="请输入当期实际收到的款项总额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="当期未收回的款项余额" prop="receivableBalance">
        <el-input
          v-model="queryParams.receivableBalance"
          placeholder="请输入当期未收回的款项余额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户提前支付的款项（如预付款）" prop="advancePayment">
        <el-input
          v-model="queryParams.advancePayment"
          placeholder="请输入客户提前支付的款项（如预付款）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="记录创建的具体时间（年-月-日 时:分:秒），默认为当前时间" prop="creationTime">
        <el-date-picker
          v-model="queryParams.creationTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="布尔值，表示该记录是否处于启用状态（如 TRUE 表示启用，FALSE 表示停用），默认为 TRUE" prop="isEnabled">
        <el-select
          v-model="queryParams.isEnabled"
          placeholder="请选择布尔值，表示该记录是否处于启用状态（如 TRUE 表示启用，FALSE 表示停用），默认为 TRUE"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['customer:contract-info:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['customer:contract-info:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="企业编号" prop="enterpriseCode" align="center" />
      <el-table-column label="企业名称" prop="enterpriseName" align="center" />
      <el-table-column label="来源" prop="source" align="center" />
      <el-table-column label="客户分类" prop="customerCategory" align="center" />
      <el-table-column label="客户等级" prop="customerLevel" align="center" />
      <el-table-column label="联系人" prop="contactPerson" align="center" />
      <el-table-column label="手机号" prop="mobileNumber" align="center" />
      <el-table-column label="客户负责人" prop="customerManager" align="center" />
      <el-table-column label="客户简述" prop="customerDescription" align="center" />
      <el-table-column label="合同金额" prop="contractAmount" align="center" />
      <el-table-column label="期初应收" prop="initialReceivable" align="center" />
      <el-table-column label="期初应收余额" prop="initialReceivableBalance" align="center" />
      <el-table-column label="应收金额" prop="receivableAmount" align="center" />
      <el-table-column label="已收金额" prop="receivedAmount" align="center" />
      <el-table-column label="应收余额" prop="receivableBalance" align="center" />
      <el-table-column label="预收款余额" prop="advancePayment" align="center" />
      <el-table-column label="创建人" prop="creator" align="center" />
      <el-table-column label="创建时间" prop="creationTime" align="center" :formatter="dateFormatter" width="180px" />
      <el-table-column label="是否启用" prop="isEnabled" align="center" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row)">编辑</el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ContractInfoForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ContractInfoApi, ContractInfoVO } from '@/api/customer/contractinfo'
import ContractInfoForm from './ContractInfoForm.vue'

/** 客户管理 列表 */
defineOptions({ name: 'ContractInfo' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ContractInfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  enterpriseCode: undefined,
  enterpriseName: undefined,
  source: undefined,
  customerCategory: undefined,
  customerLevel: undefined,
  contactPerson: undefined,
  mobileNumber: undefined,
  customerManager: undefined,
  customerDescription: undefined,
  contractAmount: undefined,
  initialReceivable: undefined,
  receivableAmount: undefined,
  receivedAmount: undefined,
  receivableBalance: undefined,
  advancePayment: undefined,
  creationTime: [],
  isEnabled: undefined,
  operationDescription: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ContractInfoApi.getContractInfoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, row?: any) => {
  console.log('编辑数据：', type, row)
  formRef.value.open(type, row)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ContractInfoApi.deleteContractInfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ContractInfoApi.exportContractInfo(queryParams)
    download.excel(data, '客户管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>